package window.childTab;

import window.utils.changeListColor;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class payloadTab {
    private JPanel jPanel;
    private JSplitPane jSplitPanel1;
    private JSplitPane jSplitPanel2;
    private JSplitPane YCenterSplitPanel;
    private JPanel oneCenterPanel;
    private JPanel oneEastPanel;
    private JPanel eastCenterPanel;
    private JPanel buttonWestPanel;
    private JPanel buttonInsidePanel;
    private JPanel listCenterPanel;
    private JPanel inputChoosePanel;
    private JPanel addInputPanel;

    public payloadTab() {
        jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jSplitPanel1 = new JSplitPane();
        jSplitPanel2 = new JSplitPane();
        YCenterSplitPanel = new JSplitPane();
        oneCenterPanel = new JPanel();
        oneEastPanel = new JPanel();
        buttonWestPanel = new JPanel();
        buttonInsidePanel = new JPanel();
        listCenterPanel = new JPanel();
        inputChoosePanel = new JPanel();
        addInputPanel = new JPanel();
        eastCenterPanel = new JPanel();
    }

    public JPanel getjPanel() {
        return jPanel;
    }

    /**
     * 创建空白分隔行（lines）
     * @return
     */
    public JLabel createEmptyLabel(int num) {
        String emptylines ="";
        for(int i=0; i<num; i++){
            emptylines += "<br>";
        }
        JLabel emptyLabel = new JLabel("<html>" + emptylines + "</html>");
        return emptyLabel;
    }

    /**
     * 创建空白分隔符号（1 line）
     * @return
     */
    public String createEmptyWords(int num) {
        String emptylines ="";
        for(int i=0; i<num; i++){
            emptylines += "     ";
        }
        return emptylines;
    }

    /**
     * 设置payloadTab的面板布局
     */
    public void setLayout() {
        //布局
        oneCenterPanel.setLayout(new BorderLayout());
        oneEastPanel.setLayout(new BorderLayout());
        eastCenterPanel.setLayout(new BoxLayout(eastCenterPanel, BoxLayout.Y_AXIS));
        buttonWestPanel.setLayout(new BorderLayout());
        buttonInsidePanel.setLayout(new BoxLayout(buttonInsidePanel, BoxLayout.Y_AXIS));
        listCenterPanel.setLayout(new BorderLayout());
        inputChoosePanel.setLayout(new BoxLayout(inputChoosePanel, BoxLayout.Y_AXIS));
        addInputPanel.setLayout(new BoxLayout(addInputPanel, BoxLayout.X_AXIS));

        // 按钮
        JButton addButton = new JButton("add");
        JButton removeButton = new JButton("remove");
        JButton loadButton = new JButton("load");
        JButton clearButton = new JButton("clear");
        JButton attackButton = new JButton("  start attack  ");
        attackButton.setForeground(new Color(255,255,255));
        attackButton.setBackground(new Color(255,102,51));
        attackButton.setFont(new Font("Arial",1,17));
        attackButton.setMargin(new Insets(0,0,0,0));
        attackButton.setBorderPainted(false);
        attackButton.setFocusPainted(false);

        // 列表+输入框
        String payloadList[] = { "1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20" };
        DefaultListModel defaultListModel = new DefaultListModel();
        for (String s : payloadList) {
            defaultListModel.addElement(s);
        }
        JList<String> list = new JList<String>(defaultListModel);
        list.setCellRenderer(new changeListColor());
        list.setVisibleRowCount(8);
        JScrollPane s = new JScrollPane(list);
        s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        // payload输入框
        JTextField inputFiled = new JTextField();
        inputFiled.setText("enter a new item");
        inputFiled.setForeground(new Color(213,213,213));

        // 下拉框
        JComboBox cmb=new JComboBox();    //创建JComboBox
        cmb.addItem("Add from list ...");    //向下拉列表中添加一项
        cmb.addItem("gfaf");
        cmb.addItem("adklp");
        cmb.addItem("mzmkljg");

        // 前后缀+输入
        JLabel prefixLable = new JLabel("prefix:");
        JLabel suffixLable = new JLabel("suffix:");
        JTextField inputPreFiled = new JTextField();
        JTextField inputStuFiled = new JTextField();

        // 前/后缀面板设置
        JPanel prefixPanel = new JPanel();
        prefixPanel.setLayout(new BoxLayout(prefixPanel, BoxLayout.X_AXIS));
        prefixPanel.add(prefixLable);
        prefixPanel.add(new JLabel(" "));
        prefixPanel.add(inputPreFiled);
        JPanel suffixPanel = new JPanel();
        suffixPanel.setLayout(new BoxLayout(suffixPanel, BoxLayout.X_AXIS));
        suffixPanel.add(suffixLable);
        suffixPanel.add(new JLabel(" "));
        suffixPanel.add(inputStuFiled);

        // 按钮里面板设置
        buttonInsidePanel.add(loadButton);
        buttonInsidePanel.add(createEmptyLabel(1));
        buttonInsidePanel.add(clearButton);
        buttonInsidePanel.add(createEmptyLabel(1));
        buttonInsidePanel.add(removeButton);
//        buttonInsidePanel.add(addButton);

        // 按钮外面板设置
        buttonWestPanel.add(buttonInsidePanel, BorderLayout.CENTER);
        buttonWestPanel.add(new JPanel(), BorderLayout.EAST);

        // 按钮事件监听
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String addStr = inputFiled.getText();
                defaultListModel.addElement(addStr);
            }
        });

        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                defaultListModel.removeElement(list.getSelectedValue());
            }
        });

        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                defaultListModel.removeAllElements();
            }
        });

//        loadButton.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//
//            }
//        });

        // add按钮+输入框面板设置
        addInputPanel.add(addButton);
        addInputPanel.add(inputFiled);

        // 输入框+下拉框面板设置
        inputChoosePanel.add(createEmptyLabel(1));
        inputChoosePanel.add(addInputPanel);
        inputChoosePanel.add(createEmptyLabel(1));
        inputChoosePanel.add(cmb);

        // 列表面板设置
        listCenterPanel.add(s, BorderLayout.CENTER);
        listCenterPanel.add(inputChoosePanel, BorderLayout.SOUTH);

        // 前后缀里面板设置
        eastCenterPanel.add(prefixPanel);
        eastCenterPanel.add(createEmptyLabel(1));
        eastCenterPanel.add(suffixPanel);

        // 前后缀外面板设置
        JPanel tempPanel = new JPanel();
        tempPanel.setLayout(new BorderLayout());
        tempPanel.add(eastCenterPanel, BorderLayout.NORTH);
        oneEastPanel.add(tempPanel, BorderLayout.CENTER);
        oneEastPanel.add(new JPanel(), BorderLayout.WEST);

        // payload面板设置
        oneCenterPanel.add(buttonWestPanel, BorderLayout.WEST);
        oneCenterPanel.add(listCenterPanel, BorderLayout.CENTER);

        // Y轴的中心面板设置
        YCenterSplitPanel.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        YCenterSplitPanel.setDividerSize(0);
        YCenterSplitPanel.setLeftComponent(oneCenterPanel);
        YCenterSplitPanel.setRightComponent(oneEastPanel);
        YCenterSplitPanel.setResizeWeight(2/3d);

        // Y轴split1分离面板设置
        JPanel attackInsidePanel = new JPanel();
        JPanel attackPanel = new JPanel();
        attackInsidePanel.setLayout(new BoxLayout(attackInsidePanel, BoxLayout.Y_AXIS));
        attackInsidePanel.add(createEmptyLabel(1));
        attackInsidePanel.add(attackButton);
        attackPanel.setLayout(new BorderLayout());
        attackPanel.add(attackInsidePanel, BorderLayout.WEST);
        jSplitPanel1.setOrientation(JSplitPane.VERTICAL_SPLIT);
        jSplitPanel1.setDividerSize(0);
        jSplitPanel1.setLeftComponent(attackPanel);
        jSplitPanel1.setRightComponent(YCenterSplitPanel);
        jSplitPanel1.setResizeWeight(3/4d);

        // Y轴split2分离面板设置
        jSplitPanel2.setOrientation(JSplitPane.VERTICAL_SPLIT);
        jSplitPanel2.setDividerSize(0);
        jSplitPanel2.setLeftComponent(jSplitPanel1);
        jSplitPanel2.setRightComponent(new JPanel());
        jSplitPanel2.setResizeWeight(1/4d);

        // 总体面板设置
        jPanel.add(jSplitPanel2);
    }
}